package com.liws.helloThreadPools.service;

import com.liws.helloThreadPools.utils.FactorialUtils;
import com.liws.helloThreadPools.utils.ThreadPoolUtils;
import sun.nio.ch.ThreadPool;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;


/**
 * @author ：liweisheng
 * @date ：2021/12/26 14:22
 * @description：自定义线程池
 */
public class ThreadPoolService {

    private static final Long num=6L;
    public static void main(String[] args) {

        Long temp=num;
        ThreadPoolService threadPoolService = new ThreadPoolService();

        // 多线程
        Instant threadBeginTime = java.time.Instant.now();


        List<Long> threadPoolsResult = threadPoolService.getThreadPoolsResult(temp);

        int threadPoolSize = threadPoolsResult.size();
        System.out.println("多线程最后一个："+threadPoolsResult.get(threadPoolSize-1));
        threadPoolsResult.forEach(s->{
            String s1 = s.toString();
        });

        Instant threadEndTime = java.time.Instant.now();

        System.out.println("多线程："+Duration.between(threadBeginTime, threadEndTime).toMillis()+" ms");


        // 单线程
        Instant commonsBeginTime = java.time.Instant.now();

        List<Long> commonsResult = threadPoolService.getCommonsResult(temp);
        int commonsSize = commonsResult.size();
        System.out.println("单线程最后一个："+commonsResult.get(commonsSize-1));

        commonsResult.forEach(s->{
            String s1 = s.toString();
        });

        Instant commonsEndTime = java.time.Instant.now();

        System.out.println("单线程："+Duration.between(commonsBeginTime, commonsEndTime).toMillis()+" ms");


    }

    /**
     *  多线程跑阶乘
     * @param temp
     * @return
     */
    public List<Long> getThreadPoolsResult(Long temp){
        ThreadPoolUtils instance = ThreadPoolUtils.getInstance();

        ExecutorService executorService = instance.getExecutorService();



        List<Future<Long>> futures  =null;
        List<MyTask> taskList=new ArrayList<>();
        for (Long i=1L;i<=temp;i++){

            taskList.add(new MyTask(i));
        }



        try {
            futures = executorService.invokeAll(taskList);


        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<Long> resultList = new ArrayList<>();

        Iterator<MyTask> taskIter = taskList.iterator();
        for (Future<Long> future : futures) {
            MyTask task = taskIter.next();
            try {
                resultList.add(future.get());
            } catch (ExecutionException e) {
                e.printStackTrace();
                System.out.println("任务异常，案件号是" + task.num);
            } catch (CancellationException e) {
                e.printStackTrace();
                System.out.println("任务超时，案件号是" + task.num);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        executorService.shutdown();

        return resultList;
    }

    /**
     *  普通跑阶乘
     * @param temp
     */
    public List<Long> getCommonsResult(Long temp){
        // 模拟耗时操作
        try {
            Thread.sleep(300000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Long sum=0L;
        List<Long> result=new ArrayList<>();


        for (Long j=1L;j<=temp;j++){
            Long aLong = FactorialUtils.countFactorial(j);
            sum+=aLong;
            result.add(sum);

        }
        return result;
    }


}
